package com.zscat.mallplus.dms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.dms.entity.Banner;
import com.zscat.mallplus.dms.service.IDmsBannerService;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@RestController
@Api(tags = "DmsBannerController", description = "首页轮播广告表管理")
@RequestMapping("/admin/dms/banner")
public class DmsBannerController {

    @Resource
    private IDmsBannerService bannerService;

    @SysLog(MODULE = "dms", REMARK = "根据条件查询所有首页轮播广告表列表")
    @ApiOperation("根据条件查询所有首页轮播广告表列表")
    @GetMapping(value = "/list")
    public Object getBannerByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        SysUser user = UserUtils.getCurrentMember();
        Long storeId = user.getQsgStoreId();
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("create_datetime");
        qw.eq("store_id",storeId);
        try {
            IPage<Banner> banners = bannerService.page(new Page<>(pageNum,pageSize),qw);
            return new CommonResult().success(bannerService.page(new Page<>(pageNum, pageSize), qw));
        } catch (Exception e) {
            log.error("根据条件查询所有首页轮播广告表列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "dms", REMARK = "保存首页广告表")
    @ApiOperation("保存首页广告表")
    @PostMapping(value = "/create")
    public Object saveBanner(@RequestBody Banner entity) {
        try {
            SysUser user = UserUtils.getCurrentMember();
            Long storeId = user.getQsgStoreId();
            if (storeId!=164&&entity.getIsZuoti()==0){
                return new CommonResult().success();
            }
            entity.setStoreId(storeId);
            entity.setCreateDatetime(new Date());
            if (bannerService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存首页广告表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "dms", REMARK = "更新首页广告表")
    @ApiOperation("更新首页广告表")
    @PostMapping(value = "/update/{id}")
    public Object updateBanner(@RequestBody Banner entity) {
        try {
            SysUser user = UserUtils.getCurrentMember();
            Long storeId = user.getQsgStoreId();
            if (storeId!=164&&entity.getIsZuoti()==0){
                return new CommonResult().success();
            }
            if (bannerService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新首页广告表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "dms", REMARK = "删除首页广告表")
    @ApiOperation("删除首页广告表")
    @GetMapping(value = "/delete/{id}")
    public Object deleteBanner(@ApiParam("首页广告表id") @PathVariable Long id) {
        try {
            SysUser user = UserUtils.getCurrentMember();
            Long storeId = user.getQsgStoreId();
            Banner entity = bannerService.getById(id);
            if (storeId!=164&&entity.getIsZuoti()==0){
                return new CommonResult().success();
            }
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("首页广告表id");
            }
            if (bannerService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除首页广告表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "dms", REMARK = "给首页广告表分配首页广告表")
    @ApiOperation("查询首页广告表明细")
    @GetMapping(value = "/{id}")
    public Object getBannerById(@ApiParam("首页广告表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("首页广告表id");
            }
            Banner banner = bannerService.getById(id);
            return new CommonResult().success(banner);
        } catch (Exception e) {
            log.error("查询首页广告表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }

    }
}
